package com.biz.primus.ms.payment.dao.specification;

import com.biz.primus.model.payment.vo.req.PaymentChannelQueryVo;
import com.biz.primus.ms.base.jpa.specification.CustomSpecification;
import com.biz.primus.ms.payment.dao.po.PaymentChannel;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by defei on 9/26/17.
 */
public class PaymentChannelQueryVoToSpecification extends CustomSpecification<PaymentChannelQueryVo, PaymentChannel> {

    public PaymentChannelQueryVoToSpecification(PaymentChannelQueryVo queryVo) {
        super(queryVo);
    }

    @Override
    public Predicate toPredicate(Root<PaymentChannel> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

        if (queryVo == null) {
            return null;
        }
        List<Predicate> predicates = newArrayList();
        if (queryVo.getMerchantId() != null) {
            predicates.add(criteriaBuilder.equal(root.get("merchant").get("id"), queryVo.getMerchantId()));
        }
        if (queryVo.getPaymentType() != null) {
            predicates.add(criteriaBuilder.equal(root.get("paymentType"), queryVo.getPaymentType()));
        }
        if (predicates.isEmpty()) {
            return null;
        } else {
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    }
}
